{extend name="common@base/common" /}

{block name="style"}
<link href="__CSS__/main.min.css" rel="stylesheet" type="text/css"/>

{/block}

{block name="body"}
<div class="container">
  <ol class="breadcrumb">
      <li><a href="{:url('index/index')}">演示首页</a></li>
      <li>拖动</li>
  </ol>
<section class="demo-section">
<div id="pageContent">



<article><p>拖动插件可以方便的允许一个元素在父级容器内通过拖动操作来更改位置。</p><div class="alert alert-danger">
  <h4>兼容性问题</h4>
  <p>在触摸屏上无法支持拖拽功能。</p>
</div><p><strong>被拖动的元素需要设置 CSS <code>position</code> 为 <code>absolute</code></strong>。当元素被拖动时，其位置是相对文档 <code>&lt;body&gt;</code> 来计算的，也可以通过设置 <code>container</code> 选项来指定计算位置时所使用的参考元素。</p><p>使用 <code>$().draggable()</code> 来为被拖动的元素进行初始化。</p></article>

<style>
#draggableBox {
    height: 340px;
    background-color: #fafafa;
    position: relative;
}
#draggableBtn {
    position: absolute;
    top: 10px;
    left: 10px;
    transition: none;
}
#dragLog {
    margin: 0;
    border: none;
    background: none;
}
</style>



<script>
$(function() {
    var count = 0;
    var $dragLog = $('#dragLog');
    var $btnPosition = $('#printPosition');
    $('#draggableBtn').draggable({
        container: '#draggableBox',
        before: function() {
            $dragLog.prepend(count++ + ': ' + '[开始] 拖动...\n');
        },
        drag: function(e) {
            $dragLog.prepend(count++ + ': ' + '拖动: pos = ' + JSON.stringify(e.pos) + ', offset = ' + JSON.stringify(e.offset) + '\n');
            $btnPosition.text('(' + e.pos.left + ', ' + e.pos.top + ')');
        },
        finish: function(e) {
            $dragLog.prepend(count++ + ': ' + '[完毕]：pos = ' + JSON.stringify(e.pos) + ', offset = ' + JSON.stringify(e.offset) + '\n');
        }
    });
});
</script>

<style>
#draggableBox2 {
    height: 340px;
    background-color: #fafafa;
    position: relative;
}
#draggableBox2 .btn-draggable {
    position: absolute;
    transition: none;
    cursor: move;
}
#dragLog2 {
    margin: 0;
    border: none;
    background: none;
}
</style>

<script>
$(function() {
    var count = 0;
    var $dragLog = $('#dragLog2');
    $('#draggableBox2').draggable({
        container: '#draggableBox2',
        selector: '.btn-draggable',
        before: function(e) {
            $dragLog.prepend(count++ + ': ' + '[开始] 拖动【按钮#' + e.element.find('.btn-draggable-id').text() + '】...\n');
        },
        drag: function(e) {
            $dragLog.prepend(count++ + ': ' + '拖动【按钮#' + e.element.find('.btn-draggable-id').text() + '】: pos = ' + JSON.stringify(e.pos) + ', offset = ' + JSON.stringify(e.offset) + '\n');
        },
        finish: function(e) {
            $dragLog.prepend(count++ + ': ' + '[完毕]【按钮#' + e.element.find('.btn-draggable-id').text() + '】：pos = ' + JSON.stringify(e.pos) + ', offset = ' + JSON.stringify(e.offset) + '\n');
        }
    });
});
</script>

<section><header><h3>示例</h3></header><article><h4>简单应用</h4><div class="example">
  <div id="draggableBox">
    <pre id="dragLog" class="pre-scrollable prettyprint prettyprinted" style=""></pre>
    <button id="draggableBtn" type="button" class="btn btn-primary"><i class="icon-move"></i> <span id="printPosition">拖动我</span></button>
  </div>
</div><pre class="prettyprint prettyprinted" style=""><code class="lang-html"><span class="tag">&lt;div</span><span class="pln"> </span><span class="atn">id</span><span class="pun">=</span><span class="atv">"draggableBox"</span><span class="tag">&gt;</span><span class="pln">
  </span><span class="tag">&lt;pre</span><span class="pln"> </span><span class="atn">id</span><span class="pun">=</span><span class="atv">"dragLog"</span><span class="pln"> </span><span class="atn">class</span><span class="pun">=</span><span class="atv">"pre-scrollable"</span><span class="tag">&gt;</span><span class="pln">用于输出演示日志信息</span><span class="tag">&lt;/pre&gt;</span><span class="pln">
  </span><span class="tag">&lt;button</span><span class="pln"> </span><span class="atn">id</span><span class="pun">=</span><span class="atv">"draggableBtn"</span><span class="pln"> </span><span class="atn">type</span><span class="pun">=</span><span class="atv">"button"</span><span class="pln"> </span><span class="atn">class</span><span class="pun">=</span><span class="atv">"btn btn-primary"</span><span class="tag">&gt;&lt;i</span><span class="pln"> </span><span class="atn">class</span><span class="pun">=</span><span class="atv">"icon-move"</span><span class="tag">&gt;&lt;/i&gt;</span><span class="pln"> </span><span class="tag">&lt;span</span><span class="pln"> </span><span class="atn">id</span><span class="pun">=</span><span class="atv">"printPosition"</span><span class="tag">&gt;</span><span class="pln">拖动我</span><span class="tag">&lt;/span&gt;&lt;/button&gt;</span><span class="pln">
</span><span class="tag">&lt;/div&gt;</span></code></pre><pre class="prettyprint prettyprinted" style=""><code class="lang-css"><span class="com">#draggableBox {</span><span class="pln">
    height</span><span class="pun">:</span><span class="pln"> </span><span class="lit">340px</span><span class="pun">;</span><span class="pln">
    background</span><span class="pun">-</span><span class="pln">color</span><span class="pun">:</span><span class="pln"> </span><span class="com">#fafafa;</span><span class="pln">
    position</span><span class="pun">:</span><span class="pln"> relative</span><span class="pun">;</span><span class="pln">
</span><span class="pun">}</span><span class="pln">
</span><span class="com">#draggableBtn {</span><span class="pln">
    position</span><span class="pun">:</span><span class="pln"> absolute</span><span class="pun">;</span><span class="pln"> </span><span class="com">/* 被拖动的元素必须设置 position 为 absolute */</span><span class="pln">
    top</span><span class="pun">:</span><span class="pln"> </span><span class="lit">10px</span><span class="pun">;</span><span class="pln">
    left</span><span class="pun">:</span><span class="pln"> </span><span class="lit">10px</span><span class="pun">;</span><span class="pln">
</span><span class="pun">}</span></code></pre><pre class="prettyprint prettyprinted" style=""><code class="lang-js"><span class="kwd">var</span><span class="pln"> count </span><span class="pun">=</span><span class="pln"> </span><span class="lit">0</span><span class="pun">;</span><span class="pln"> </span><span class="com">// 用于标记日志输出顺序</span><span class="pln">
</span><span class="kwd">var</span><span class="pln"> $dragLog </span><span class="pun">=</span><span class="pln"> $</span><span class="pun">(</span><span class="str">'#dragLog'</span><span class="pun">);</span><span class="pln">
</span><span class="kwd">var</span><span class="pln"> $btnPosition </span><span class="pun">=</span><span class="pln"> $</span><span class="pun">(</span><span class="str">'#printPosition'</span><span class="pun">);</span><span class="pln">
$</span><span class="pun">(</span><span class="str">'#draggableBtn'</span><span class="pun">).</span><span class="pln">draggable</span><span class="pun">({</span><span class="pln">
    container</span><span class="pun">:</span><span class="pln"> </span><span class="str">'#draggableBox'</span><span class="pun">,</span><span class="pln">
    before</span><span class="pun">:</span><span class="pln"> </span><span class="kwd">function</span><span class="pun">()</span><span class="pln"> </span><span class="pun">{</span><span class="pln">
        $dragLog</span><span class="pun">.</span><span class="pln">prepend</span><span class="pun">(</span><span class="pln">count</span><span class="pun">++</span><span class="pln"> </span><span class="pun">+</span><span class="pln"> </span><span class="str">': '</span><span class="pln"> </span><span class="pun">+</span><span class="pln"> </span><span class="str">'[开始] 拖动...\n'</span><span class="pun">);</span><span class="pln">
    </span><span class="pun">},</span><span class="pln">
    drag</span><span class="pun">:</span><span class="pln"> </span><span class="kwd">function</span><span class="pun">(</span><span class="pln">e</span><span class="pun">)</span><span class="pln"> </span><span class="pun">{</span><span class="pln">
        $dragLog</span><span class="pun">.</span><span class="pln">prepend</span><span class="pun">(</span><span class="pln">count</span><span class="pun">++</span><span class="pln"> </span><span class="pun">+</span><span class="pln"> </span><span class="str">': '</span><span class="pln"> </span><span class="pun">+</span><span class="pln"> </span><span class="str">'拖动: pos = '</span><span class="pln"> </span><span class="pun">+</span><span class="pln"> JSON</span><span class="pun">.</span><span class="pln">stringify</span><span class="pun">(</span><span class="pln">e</span><span class="pun">.</span><span class="pln">pos</span><span class="pun">)</span><span class="pln"> </span><span class="pun">+</span><span class="pln"> </span><span class="str">', offset = '</span><span class="pln"> </span><span class="pun">+</span><span class="pln"> JSON</span><span class="pun">.</span><span class="pln">stringify</span><span class="pun">(</span><span class="pln">e</span><span class="pun">.</span><span class="pln">offset</span><span class="pun">)</span><span class="pln"> </span><span class="pun">+</span><span class="pln"> </span><span class="str">'\n'</span><span class="pun">);</span><span class="pln">
        $btnPosition</span><span class="pun">.</span><span class="pln">text</span><span class="pun">(</span><span class="str">'('</span><span class="pln"> </span><span class="pun">+</span><span class="pln"> e</span><span class="pun">.</span><span class="pln">pos</span><span class="pun">.</span><span class="pln">left </span><span class="pun">+</span><span class="pln"> </span><span class="str">', '</span><span class="pln"> </span><span class="pun">+</span><span class="pln"> e</span><span class="pun">.</span><span class="pln">pos</span><span class="pun">.</span><span class="pln">top </span><span class="pun">+</span><span class="pln"> </span><span class="str">')'</span><span class="pun">);</span><span class="pln">
    </span><span class="pun">},</span><span class="pln">
    finish</span><span class="pun">:</span><span class="pln"> </span><span class="kwd">function</span><span class="pun">(</span><span class="pln">e</span><span class="pun">)</span><span class="pln"> </span><span class="pun">{</span><span class="pln">
        $dragLog</span><span class="pun">.</span><span class="pln">prepend</span><span class="pun">(</span><span class="pln">count</span><span class="pun">++</span><span class="pln"> </span><span class="pun">+</span><span class="pln"> </span><span class="str">': '</span><span class="pln"> </span><span class="pun">+</span><span class="pln"> </span><span class="str">'[完毕]：pos = '</span><span class="pln"> </span><span class="pun">+</span><span class="pln"> JSON</span><span class="pun">.</span><span class="pln">stringify</span><span class="pun">(</span><span class="pln">e</span><span class="pun">.</span><span class="pln">pos</span><span class="pun">)</span><span class="pln"> </span><span class="pun">+</span><span class="pln"> </span><span class="str">', offset = '</span><span class="pln"> </span><span class="pun">+</span><span class="pln"> JSON</span><span class="pun">.</span><span class="pln">stringify</span><span class="pun">(</span><span class="pln">e</span><span class="pun">.</span><span class="pln">offset</span><span class="pun">)</span><span class="pln"> </span><span class="pun">+</span><span class="pln"> </span><span class="str">'\n'</span><span class="pun">);</span><span class="pln">
    </span><span class="pun">}</span><span class="pln">
</span><span class="pun">});</span></code></pre><h4>容器内多个元素拖动</h4><div class="example">
  <div id="draggableBox2">
    <pre id="dragLog2" class="pre-scrollable prettyprint prettyprinted" style=""><span class="pun">用于输出演示日志信息</span></pre>
    <button type="button" class="btn btn-primary btn-draggable" style="top: 10px; left: 10px;"><i class="icon-move"></i> 按钮 #<span class="btn-draggable-id">1</span></button>
    <button type="button" class="btn btn-danger btn-draggable" style="top: 60px; left: 10px;"><i class="icon-move"></i> 按钮 #<span class="btn-draggable-id">2</span></button>
    <button type="button" class="btn btn-success btn-draggable" style="top: 110px; left: 110px;"><i class="icon-move"></i> 按钮 #<span class="btn-draggable-id">3</span></button>
    <button type="button" class="btn btn-warning btn-draggable" style="top: 160px; left: 210px;"><i class="icon-move"></i> 按钮 #<span class="btn-draggable-id">4</span></button>
  </div>
</div><pre class="prettyprint prettyprinted" style=""><code class="lang-html"><span class="tag">&lt;div</span><span class="pln"> </span><span class="atn">id</span><span class="pun">=</span><span class="atv">"draggableBox2"</span><span class="tag">&gt;</span><span class="pln">
  </span><span class="tag">&lt;pre</span><span class="pln"> </span><span class="atn">id</span><span class="pun">=</span><span class="atv">"dragLog2"</span><span class="pln"> </span><span class="atn">class</span><span class="pun">=</span><span class="atv">"pre-scrollable"</span><span class="tag">&gt;</span><span class="pln">用于输出演示日志信息</span><span class="tag">&lt;/pre&gt;</span><span class="pln">
  </span><span class="tag">&lt;button</span><span class="pln"> </span><span class="atn">type</span><span class="pun">=</span><span class="atv">"button"</span><span class="pln"> </span><span class="atn">class</span><span class="pun">=</span><span class="atv">"btn btn-primary btn-draggable"</span><span class="pln"> </span><span class="atn">style</span><span class="pun">=</span><span class="atv">"</span><span class="pln">top</span><span class="pun">:</span><span class="pln"> </span><span class="lit">10px</span><span class="pun">;</span><span class="pln"> left</span><span class="pun">:</span><span class="pln"> </span><span class="lit">10px</span><span class="pun">;</span><span class="atv">"</span><span class="tag">&gt;&lt;i</span><span class="pln"> </span><span class="atn">class</span><span class="pun">=</span><span class="atv">"icon-move"</span><span class="tag">&gt;&lt;/i&gt;</span><span class="pln"> 按钮 #</span><span class="tag">&lt;span</span><span class="pln"> </span><span class="atn">class</span><span class="pun">=</span><span class="atv">"btn-draggable-id"</span><span class="tag">&gt;</span><span class="pln">1</span><span class="tag">&lt;/span&gt;&lt;/button&gt;</span><span class="pln">
  </span><span class="tag">&lt;button</span><span class="pln"> </span><span class="atn">type</span><span class="pun">=</span><span class="atv">"button"</span><span class="pln"> </span><span class="atn">class</span><span class="pun">=</span><span class="atv">"btn btn-danger btn-draggable"</span><span class="pln"> </span><span class="atn">style</span><span class="pun">=</span><span class="atv">"</span><span class="pln">top</span><span class="pun">:</span><span class="pln"> </span><span class="lit">60px</span><span class="pun">;</span><span class="pln"> left</span><span class="pun">:</span><span class="pln"> </span><span class="lit">10px</span><span class="pun">;</span><span class="atv">"</span><span class="tag">&gt;&lt;i</span><span class="pln"> </span><span class="atn">class</span><span class="pun">=</span><span class="atv">"icon-move"</span><span class="tag">&gt;&lt;/i&gt;</span><span class="pln"> 按钮 #</span><span class="tag">&lt;span</span><span class="pln"> </span><span class="atn">class</span><span class="pun">=</span><span class="atv">"btn-draggable-id"</span><span class="tag">&gt;</span><span class="pln">2</span><span class="tag">&lt;/span&gt;&lt;/button&gt;</span><span class="pln">
  </span><span class="tag">&lt;button</span><span class="pln"> </span><span class="atn">type</span><span class="pun">=</span><span class="atv">"button"</span><span class="pln"> </span><span class="atn">class</span><span class="pun">=</span><span class="atv">"btn btn-success btn-draggable"</span><span class="pln"> </span><span class="atn">style</span><span class="pun">=</span><span class="atv">"</span><span class="pln">top</span><span class="pun">:</span><span class="pln"> </span><span class="lit">110px</span><span class="pun">;</span><span class="pln"> left</span><span class="pun">:</span><span class="pln"> </span><span class="lit">110px</span><span class="pun">;</span><span class="atv">"</span><span class="tag">&gt;&lt;i</span><span class="pln"> </span><span class="atn">class</span><span class="pun">=</span><span class="atv">"icon-move"</span><span class="tag">&gt;&lt;/i&gt;</span><span class="pln"> 按钮 #</span><span class="tag">&lt;span</span><span class="pln"> </span><span class="atn">class</span><span class="pun">=</span><span class="atv">"btn-draggable-id"</span><span class="tag">&gt;</span><span class="pln">3</span><span class="tag">&lt;/span&gt;&lt;/button&gt;</span><span class="pln">
  </span><span class="tag">&lt;button</span><span class="pln"> </span><span class="atn">type</span><span class="pun">=</span><span class="atv">"button"</span><span class="pln"> </span><span class="atn">class</span><span class="pun">=</span><span class="atv">"btn btn-warning btn-draggable"</span><span class="pln"> </span><span class="atn">style</span><span class="pun">=</span><span class="atv">"</span><span class="pln">top</span><span class="pun">:</span><span class="pln"> </span><span class="lit">160px</span><span class="pun">;</span><span class="pln"> left</span><span class="pun">:</span><span class="pln"> </span><span class="lit">210px</span><span class="pun">;</span><span class="atv">"</span><span class="tag">&gt;&lt;i</span><span class="pln"> </span><span class="atn">class</span><span class="pun">=</span><span class="atv">"icon-move"</span><span class="tag">&gt;&lt;/i&gt;</span><span class="pln"> 按钮 #</span><span class="tag">&lt;span</span><span class="pln"> </span><span class="atn">class</span><span class="pun">=</span><span class="atv">"btn-draggable-id"</span><span class="tag">&gt;</span><span class="pln">4</span><span class="tag">&lt;/span&gt;&lt;/button&gt;</span><span class="pln">
</span><span class="tag">&lt;/div&gt;</span></code></pre><pre class="prettyprint prettyprinted" style=""><code class="lang-css"><span class="com">#draggableBox2 {</span><span class="pln">
    height</span><span class="pun">:</span><span class="pln"> </span><span class="lit">340px</span><span class="pun">;</span><span class="pln">
    background</span><span class="pun">-</span><span class="pln">color</span><span class="pun">:</span><span class="pln"> </span><span class="com">#fafafa;</span><span class="pln">
    position</span><span class="pun">:</span><span class="pln"> relative</span><span class="pun">;</span><span class="pln">
</span><span class="pun">}</span><span class="pln">
</span><span class="com">#draggableBox2 .btn-draggable {</span><span class="pln">
    position</span><span class="pun">:</span><span class="pln"> absolute</span><span class="pun">;</span><span class="pln">
    transition</span><span class="pun">:</span><span class="pln"> none</span><span class="pun">;</span><span class="pln">
    cursor</span><span class="pun">:</span><span class="pln"> move</span><span class="pun">;</span><span class="pln">
</span><span class="pun">}</span><span class="pln">
</span><span class="com">#dragLog2 {</span><span class="pln">
    margin</span><span class="pun">:</span><span class="pln"> </span><span class="lit">0</span><span class="pun">;</span><span class="pln">
    border</span><span class="pun">:</span><span class="pln"> none</span><span class="pun">;</span><span class="pln">
    background</span><span class="pun">:</span><span class="pln"> none</span><span class="pun">;</span><span class="pln">
</span><span class="pun">}</span></code></pre><pre class="prettyprint prettyprinted" style=""><code class="lang-js"><span class="kwd">var</span><span class="pln"> count </span><span class="pun">=</span><span class="pln"> </span><span class="lit">0</span><span class="pun">;</span><span class="pln">
</span><span class="kwd">var</span><span class="pln"> $dragLog </span><span class="pun">=</span><span class="pln"> $</span><span class="pun">(</span><span class="str">'#dragLog2'</span><span class="pun">);</span><span class="pln">
$</span><span class="pun">(</span><span class="str">'#draggableBox2'</span><span class="pun">).</span><span class="pln">draggable</span><span class="pun">({</span><span class="pln">
    container</span><span class="pun">:</span><span class="pln"> </span><span class="str">'#draggableBox2'</span><span class="pun">,</span><span class="pln">
    selector</span><span class="pun">:</span><span class="pln"> </span><span class="str">'.btn-draggable'</span><span class="pun">,</span><span class="pln"> </span><span class="com">// 可拖动元素选择器</span><span class="pln">
    before</span><span class="pun">:</span><span class="pln"> </span><span class="kwd">function</span><span class="pun">(</span><span class="pln">e</span><span class="pun">)</span><span class="pln"> </span><span class="pun">{</span><span class="pln">
        $dragLog</span><span class="pun">.</span><span class="pln">prepend</span><span class="pun">(</span><span class="pln">count</span><span class="pun">++</span><span class="pln"> </span><span class="pun">+</span><span class="pln"> </span><span class="str">': '</span><span class="pln"> </span><span class="pun">+</span><span class="pln"> </span><span class="str">'[开始] 拖动【按钮#'</span><span class="pln"> </span><span class="pun">+</span><span class="pln"> e</span><span class="pun">.</span><span class="pln">element</span><span class="pun">.</span><span class="pln">find</span><span class="pun">(</span><span class="str">'.btn-draggable-id'</span><span class="pun">).</span><span class="pln">text</span><span class="pun">()</span><span class="pln"> </span><span class="pun">+</span><span class="pln"> </span><span class="str">'】...\n'</span><span class="pun">);</span><span class="pln">
    </span><span class="pun">},</span><span class="pln">
    drag</span><span class="pun">:</span><span class="pln"> </span><span class="kwd">function</span><span class="pun">(</span><span class="pln">e</span><span class="pun">)</span><span class="pln"> </span><span class="pun">{</span><span class="pln">
        $dragLog</span><span class="pun">.</span><span class="pln">prepend</span><span class="pun">(</span><span class="pln">count</span><span class="pun">++</span><span class="pln"> </span><span class="pun">+</span><span class="pln"> </span><span class="str">': '</span><span class="pln"> </span><span class="pun">+</span><span class="pln"> </span><span class="str">'拖动【按钮#'</span><span class="pln"> </span><span class="pun">+</span><span class="pln"> e</span><span class="pun">.</span><span class="pln">element</span><span class="pun">.</span><span class="pln">find</span><span class="pun">(</span><span class="str">'.btn-draggable-id'</span><span class="pun">).</span><span class="pln">text</span><span class="pun">()</span><span class="pln"> </span><span class="pun">+</span><span class="pln"> </span><span class="str">'】: pos = '</span><span class="pln"> </span><span class="pun">+</span><span class="pln"> JSON</span><span class="pun">.</span><span class="pln">stringify</span><span class="pun">(</span><span class="pln">e</span><span class="pun">.</span><span class="pln">pos</span><span class="pun">)</span><span class="pln"> </span><span class="pun">+</span><span class="pln"> </span><span class="str">', offset = '</span><span class="pln"> </span><span class="pun">+</span><span class="pln"> JSON</span><span class="pun">.</span><span class="pln">stringify</span><span class="pun">(</span><span class="pln">e</span><span class="pun">.</span><span class="pln">offset</span><span class="pun">)</span><span class="pln"> </span><span class="pun">+</span><span class="pln"> </span><span class="str">'\n'</span><span class="pun">);</span><span class="pln">
    </span><span class="pun">},</span><span class="pln">
    finish</span><span class="pun">:</span><span class="pln"> </span><span class="kwd">function</span><span class="pun">(</span><span class="pln">e</span><span class="pun">)</span><span class="pln"> </span><span class="pun">{</span><span class="pln">
        $dragLog</span><span class="pun">.</span><span class="pln">prepend</span><span class="pun">(</span><span class="pln">count</span><span class="pun">++</span><span class="pln"> </span><span class="pun">+</span><span class="pln"> </span><span class="str">': '</span><span class="pln"> </span><span class="pun">+</span><span class="pln"> </span><span class="str">'[完毕]【按钮#'</span><span class="pln"> </span><span class="pun">+</span><span class="pln"> e</span><span class="pun">.</span><span class="pln">element</span><span class="pun">.</span><span class="pln">find</span><span class="pun">(</span><span class="str">'.btn-draggable-id'</span><span class="pun">).</span><span class="pln">text</span><span class="pun">()</span><span class="pln"> </span><span class="pun">+</span><span class="pln"> </span><span class="str">'】：pos = '</span><span class="pln"> </span><span class="pun">+</span><span class="pln"> JSON</span><span class="pun">.</span><span class="pln">stringify</span><span class="pun">(</span><span class="pln">e</span><span class="pun">.</span><span class="pln">pos</span><span class="pun">)</span><span class="pln"> </span><span class="pun">+</span><span class="pln"> </span><span class="str">', offset = '</span><span class="pln"> </span><span class="pun">+</span><span class="pln"> JSON</span><span class="pun">.</span><span class="pln">stringify</span><span class="pun">(</span><span class="pln">e</span><span class="pun">.</span><span class="pln">offset</span><span class="pun">)</span><span class="pln"> </span><span class="pun">+</span><span class="pln"> </span><span class="str">'\n'</span><span class="pun">);</span><span class="pln">
    </span><span class="pun">}</span><span class="pln">
</span><span class="pun">});</span></code></pre></article></section>





<section><header><h3>选项</h3></header><article><p>在进行初始化时允许传入一个对象参数作为初始化选项。可以使用的选项如下：</p><div class="table-responsive"><table class="table table-bordered">
  <thead>
    <tr>
      <th>选项</th>
      <th>名称</th>
      <th>可选值</th>
      <th>描述</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td><code>container</code></td>
      <td>位置计算容器</td>
      <td>默认 <code>'body'</code></td>
      <td>使用 jQuery 选择器来指定用于计算位置的父级容器。用于父级容器的节点其 <code>position</code> 应该为 <code>'relative'</code>、<code>'absolute'</code>及<code>'fixed'</code> 中的一种。</td>
    </tr>
    <tr>
      <td><code>move</code></td>
      <td>是否移动被拖动元素</td>
      <td><code>true</code>（默认）、<code>false</code></td>
      <td>如果设置为 <code>false</code>，则拖动目标元素时并不会更改目标元素当前显示的位置，但仍然可以获得鼠标当前拖到的位置信息。</td>
    </tr>
    <tr>
      <td><code>selector</code></td>
      <td>拖动元素选择器 (1.6+)</td>
      <td>默认为 <code>""</code></td>
      <td>当设置此选项后将 <code>$()</code> 选定的元素作为容器，<code>selector</code> 选项用于指定容器内的哪些元素可以拖动。</td>
    </tr>
    <tr>
      <td><code>handle</code></td>
      <td>拖动事件触发元素选择器</td>
      <td>默认为 <code>null</code></td>
      <td>用于选择被拖动元素内部元素的选择器，如果指定该选项，则仅当鼠标在指定的子元素上点按鼠标会触发拖动事件。</td>
    </tr>
    <tr>
      <td><code>mouseButton</code></td>
      <td>响应的鼠标按钮</td>
      <td>默认为 <code>'all'</code></td>
      <td>
        <p>所有可响应的鼠标按钮参考 <a href="https://developer.mozilla.org/zh-CN/docs/Web/API/MouseEvent/button" target="_blank"><code>MouseEvent.button</code></a>：</p>
        <ul>
          <li><code>'all'</code> 或 <code>-1</code>：响应所有鼠标按键；</li>
          <li><code>'left'</code> 或 <code>0</code>：响应鼠标左键；</li>
          <li><code>'middle'</code> 或 <code>1</code>：响应鼠标滚轮（中键）；</li>
          <li><code>'right'</code> 或 <code>2</code>：响应鼠标右键。</li>
        </ul>
      </td>
    </tr>
    <tr>
      <td><code>stopPropagation</code></td>
      <td>是否阻止事件冒泡</td>
      <td><code>true</code>、<code>false</code>（默认）</td>
      <td>如果设置为 <code>true</code>，则拖动发生时与鼠标相关的事件（包括 <code>mousedown</code>、<code>mousemove</code> 和 <code>mouseup</code>）会调用 <code>event.stopPropagation()</code> 来阻止事件冒泡。</td>
    </tr>
    <tr>
      <td><code>before</code></td>
      <td>拖动前回调函数</td>
      <td>默认不设置</td>
      <td>指定一个回调函数在拖动发生前（<code>mousedown</code> 事件发生时）调用，在此回调函数中返回 <code>false</code> 来取消此次拖动操作。</td>
    </tr>
    <tr>
      <td><code>drag</code></td>
      <td>移动时回调函数</td>
      <td>默认不设置</td>
      <td>指定一个回调函数在移动目标元素时前（<code>mousemove</code> 事件发生时）调用。</td>
    </tr>
    <tr>
      <td><code>finish</code></td>
      <td>拖动完成回调函数</td>
      <td>默认不设置</td>
      <td>指定一个回调函数在拖动完成后（<code>mouseup</code> 事件发生时）调用。</td>
    </tr>
  </tbody>
</table></div><p>使用选项：</p><pre class="prettyprint prettyprinted" style=""><code class="lang-js"><span class="com">// 定义选项对象</span><span class="pln">
</span><span class="kwd">var</span><span class="pln"> options </span><span class="pun">=</span><span class="pln"> </span><span class="pun">{</span><span class="pln">
    container</span><span class="pun">:</span><span class="pln"> </span><span class="str">'body'</span><span class="pun">,</span><span class="pln">
    before</span><span class="pun">:</span><span class="pln"> </span><span class="kwd">function</span><span class="pun">()</span><span class="pln"> </span><span class="pun">{</span><span class="pln">
        console</span><span class="pun">.</span><span class="pln">log</span><span class="pun">(</span><span class="str">'拖动开始...'</span><span class="pun">);</span><span class="pln">
    </span><span class="pun">},</span><span class="pln">
    </span><span class="com">// 设置更多选项...</span><span class="pln">
</span><span class="pun">};</span><span class="pln">

</span><span class="com">// 初始化时传入选项参数</span><span class="pln">
$</span><span class="pun">(</span><span class="str">'#dragBtn'</span><span class="pun">).</span><span class="pln">draggable</span><span class="pun">(</span><span class="pln">options</span><span class="pun">)</span></code></pre></article></section>















<section><header><h3>处理拖动过程</h3></header><article><p>通过选项设置 <code>before</code>、<code>drag</code>、<code>finish</code> 的回调函数来处理整个拖动过程，并实时获取拖动的位置。</p><h4><span class="code">before(e)</span></h4><p><code>before(e)</code> 回调函数在拖动发生之前调用，用于处理拖动前的操作或者决定取消后续的拖动事件，该回调函数参数 <code>e</code> 包含的属性定义如下：</p><ul>
<li><code>e.event</code>：鼠标按下时 <code>mousedown</code> 事件参数对象；</li>
<li><code>e.element</code>：被拖动元素。</li>
</ul><p>在该回调函数中返回 <code>false</code> 会取消这次拖动操作，后续相关事件也不会发生。</p><pre class="prettyprint prettyprinted" style=""><code class="lang-js"><span class="pln">$</span><span class="pun">(</span><span class="str">'#dragBtn'</span><span class="pun">).</span><span class="pln">draggable</span><span class="pun">({</span><span class="pln">
    before</span><span class="pun">:</span><span class="pln"> </span><span class="kwd">function</span><span class="pun">(</span><span class="pln">e</span><span class="pun">)</span><span class="pln"> </span><span class="pun">{</span><span class="pln">
        console</span><span class="pun">.</span><span class="pln">log</span><span class="pun">(</span><span class="str">'现在不是拖动的好时机，取消这次拖动操作。'</span><span class="pun">);</span><span class="pln">

        </span><span class="com">// 通过返回 false 来取消这次拖动操作</span><span class="pln">
        </span><span class="kwd">return</span><span class="pln"> </span><span class="kwd">false</span><span class="pun">;</span><span class="pln">
    </span><span class="pun">}</span><span class="pln">
</span><span class="pun">})</span></code></pre><h4><span class="code">drag(e)</span></h4><p><code>drag(e)</code> 回调函数在拖动时被反复调用，该回调函数参数 <code>e</code> 包含的属性定义如下：</p><ul>
<li><code>e.event</code>：鼠标移动时 <code>mousemove</code> 事件参数对象；</li>
<li><code>e.element</code>：被拖动元素；</li>
<li><code>e.pos</code>：被拖动元素当前相对父级容器（<code>container</code> 指定）的位置；</li>
<li><code>e.offset</code>：被拖动元素当前相对于开始拖动时的位置变化；</li>
<li><code>e.smallOffset</code>：被拖动元素当前相对于上次位置发生变化时的位置变化；</li>
<li><code>e.startOffset</code>：被拖动元素在开始拖动前相对父级容器（<code>container</code> 指定）的位置变化。</li>
</ul><p>由于该回调函数会在拖动时反复调用，不应该在此回调函数中进行过多的操作。</p><h4><span class="code">finish(e)</span></h4><p><code>finish(e)</code> 回调函数在拖动发生之后调用，用于处理拖动完成后的操作，该回调函数参数 <code>e</code> 包含的属性定义如下：</p><ul>
<li><code>e.event</code>：鼠标移动时 <code>mousemove</code> 事件参数对象；</li>
<li><code>e.element</code>：被拖动元素；</li>
<li><code>e.pos</code>：被拖动元素当前相对父级容器（<code>container</code> 指定）的位置；</li>
<li><code>e.offset</code>：被拖动元素当前相对于开始拖动时的位置变化；</li>
<li><code>e.smallOffset</code>：被拖动元素当前相对于上次位置发生变化时的位置变化；</li>
<li><code>e.startOffset</code>：被拖动元素在开始拖动前相对父级容器（<code>container</code> 指定）的位置变化。</li>
</ul><h4><span class="code">$().draggable('destroy')</span></h4><p>如果以确定不需要拖拽操作，则可以调用 <code>$().draggable('destroy')</code> 来销毁拖拽插件。销毁之后如果需要重新启用拖拽则重新进行初始化即可。</p><pre class="prettyprint prettyprinted" style=""><code class="lang-js"><span class="pln">$</span><span class="pun">(</span><span class="str">'#dragEles'</span><span class="pun">).</span><span class="pln">draggable</span><span class="pun">(</span><span class="str">'destroy'</span><span class="pun">);</span></code></pre></article></section></div>
{/block}

{block name="script"}

{/block}